package com.school.information.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.school.information.core.exception.ServiceException;
import com.school.information.entity.ModuleInfoEntity;
import com.school.information.enums.result.ModuleResultEnum;
import com.school.information.mapper.ModuleInfoMapper;
import com.school.information.qo.ModuleInfoQO;
import com.school.information.service.ModuleInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (ModuleInfo)表服务实现类
 *
 * @author ycc
 * @since 2023-07-04 20:36:25
 */
@Service
@Slf4j
public class ModuleInfoServiceImpl extends ServiceImpl<ModuleInfoMapper, ModuleInfoEntity> implements ModuleInfoService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveInfo(ModuleInfoEntity moduleInfo) {
        ModuleInfoEntity moduleName = getByName(moduleInfo.getName());
        log.info("##moduleName={}", moduleName);
        if (Objects.nonNull(moduleName) && StringUtils.isNotBlank(moduleName.getName())) {
            log.error("##模块名称已存在");
            throw new ServiceException(ModuleResultEnum.MODULE_INFO_NAME_EXISTS);
        }
        return this.save(moduleInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateInfo(ModuleInfoEntity moduleInfo) {
        ModuleInfoEntity moduleName = getByName(moduleInfo.getName());
        ModuleInfoEntity oldModule = getById(moduleInfo.getId());
        if (Objects.nonNull(moduleName) && StringUtils.isNotBlank(moduleInfo.getName())
                && !moduleInfo.getName().equals(oldModule.getName())) {
            log.error("##模块名称已存在");
            throw new ServiceException(ModuleResultEnum.MODULE_INFO_NAME_EXISTS);
        }
        BeanUtil.copyProperties(moduleInfo, oldModule);
        return this.updateById(oldModule);
    }

    @Override
    public IPage<ModuleInfoEntity> pageList(ModuleInfoQO moduleInfo) {
        LambdaQueryWrapper<ModuleInfoEntity> queryWrapper = Wrappers.<ModuleInfoEntity>query().lambda().orderByDesc(ModuleInfoEntity::getUpdateTime);
        if (StringUtils.isNotBlank(moduleInfo.getName())) {
            queryWrapper.like(ModuleInfoEntity::getName, moduleInfo.getName());
        }
        return this.page(new Page<>(moduleInfo.getCurrent(), moduleInfo.getSize()), queryWrapper);
    }

    @Override
    public ModuleInfoEntity getByName(String name) {
        return this.getOne(Wrappers.<ModuleInfoEntity>lambdaQuery().eq(ModuleInfoEntity::getName, name));
    }

    @Override
    public Map<Long, ModuleInfoEntity> getModuleInfoMap(List<Long> moduleInfoIds) {
        Map<Long, ModuleInfoEntity> resultMap = new HashMap<>();
        List<ModuleInfoEntity> moduleInfoList = this.listByIds(moduleInfoIds);
        moduleInfoList.forEach(moduleInfo -> {
            resultMap.put(moduleInfo.getId(), moduleInfo);
        });
        return resultMap;
    }
}
